#!/bin/sh

set -e

. ./terraform.conf

if [ "$HWE_KERNEL" = "yes" ]; then
    KERNEL_FLAVORS="generic-hwe-${BASEVERSION}"
else
    KERNEL_FLAVORS="generic"
fi

lb config noauto \
    --architectures "$ARCH" \
    --mode debian \
    --distribution "$BASECODENAME" \
    --parent-distribution "$BASECODENAME" \
    --archive-areas "main non-free" \
    --parent-archive-areas "main contrib non-free non-free-firmware" \
    --linux-packages "linux-image linux-headers" \
    --linux-flavours "$KERNEL_FLAVORS" \
    --bootappend-live "boot=live config username=vanilla user-fullname=Bee quiet splash" \
    --mirror-bootstrap "$MIRROR_URL" \
    --parent-mirror-bootstrap "$MIRROR_URL" \
    --mirror-chroot-security "http://snapshot.debian.org/archive/debian-security/20230319T030536Z" \
    --parent-mirror-chroot-security "http://snapshot.debian.org/archive/debian-security/20230319T030536Z" \
    --mirror-binary-security "http://snapshot.debian.org/archive/debian-security/20230319T030536Z" \
    --parent-mirror-binary-security "http://snapshot.debian.org/archive/debian-security/20230319T030536Z" \
    --mirror-binary "http://snapshot.debian.org/archive/debian/20230319T030536Z" \
    --parent-mirror-binary "http://snapshot.debian.org/archive/debian/20230319T030536Z" \
    --keyring-packages debian-keyring \
    --apt-options "--yes --option Acquire::Retries=5 --option Acquire::http::Timeout=100 --option=Acquire::Check-Valid-Until=false --option=Acquire::http::Dl-Limit=1000 --option=Acquire::https::Dl-Limit=1000" \
    --apt-recommends false \
    --cache-packages false \
    --uefi-secure-boot enable \
    --binary-images iso-hybrid \
    --iso-application "$NAME" \
    --iso-volume "$NAME" \
    --firmware-binary false \
    --firmware-chroot false \
    --security true \
    --updates false \
    --debootstrap-options "--exclude=pinephone-tweaks,mobile-tweaks-common,librem5-tweaks,pinetab-tweaks --include=apt-transport-https,ca-certificates,openssl" \
    --checksums md5 \
    --clean \
    "${@}"


# replace channel and suite
# sed -i "s/@CHANNEL/$CHANNEL/" config/archives/*.list*
# sed -i "s/@BASECODENAME/$BASECODENAME/" config/archives/*.list*

DATE=$(date +%Y%m%d)
sed -i "s/@CHANNEL/$CHANNEL/" config/includes.binary/.disk/info
sed -i "s/@CODENAME/$CODENAME/" config/includes.binary/.disk/info
sed -i "s/@ARCH/$ARCH/" config/includes.binary/.disk/info
sed -i "s/@DISTRO_NAME/$NAME/" config/includes.binary/.disk/info
sed -i "s/@VERSION/$VERSION/" config/includes.binary/.disk/info
sed -i "s/@DATE/$DATE/" config/includes.binary/.disk/info

sed -i "s/@DISTRO_NAME/$NAME/" config/hooks/live/004-set-os-info.chroot
sed -i "s/@VERSION/$VERSION/" config/hooks/live/004-set-os-info.chroot
sed -i "s/@CHANNEL/$CHANNEL/" config/hooks/live/004-set-os-info.chroot
sed -i "s/@CODENAME/$CODENAME/" config/hooks/live/004-set-os-info.chroot
sed -i "s/@BASECODENAME/$BASECODENAME/" config/hooks/live/004-set-os-info.chroot

# sed -i "s/@XORG_HWE/$XORG_HWE/" config/package-lists/desktop.list.chroot_install
